package com.ai.imagetext.controller;

import com.ai.imagetext.dto.OperationLogDTO;
import com.ai.imagetext.entity.GeneratedImage;
import com.ai.imagetext.entity.TextContent;
import com.ai.imagetext.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/operation-log")
@RequiredArgsConstructor
public class OperationLogController {


    private final OperationLogService operationLogService;

    /**
     * 查询用户的图生文案操作日志
     */
    @GetMapping("/user/{userId}/image-to-text")
    public ResponseEntity<List<TextContent>> getImageToTextLogsByUserId(@PathVariable Long userId) {
        List<OperationLogDTO> logs = operationLogService.findByUserIdAndType(userId,0);
        List<TextContent> textContents = new ArrayList<>();
        for(OperationLogDTO operationLogDTO : logs){
            // TODO: 根据日志信息查询对应的文本内容
            TextContent textContent = operationLogService.findContentByGeneralId(operationLogDTO.getGeneralId());
            textContents.add(textContent);
        }
        return ResponseEntity.ok(textContents);
    }

    /**
     * 查询用户的图生图操作日志
     */
    @GetMapping("/user/{userId}/image-to-image")
    public ResponseEntity<List<GeneratedImage>> getImageToImageLogsByUserId(@PathVariable Long userId) {
        List<OperationLogDTO> logs = operationLogService.findByUserIdAndType(userId,1);
        List<GeneratedImage> generatedImages = new ArrayList<>();
        for(OperationLogDTO operationLogDTO : logs){
            // TODO: 根据日志信息查询对应的生成图片
            GeneratedImage generatedImage = operationLogService.findPictureByGeneralId(operationLogDTO.getGeneralId());
            generatedImages.add(generatedImage);
        }
        return ResponseEntity.ok(generatedImages);
    }

    /**
     * 软删除操作日志
     */
    @DeleteMapping("/{logId}")
    public ResponseEntity<Void> softDelete(@PathVariable Integer logId) {
        operationLogService.softDelete(logId);
        return ResponseEntity.ok().build();
    }


    /**
     * 记录图生文操作
     */
    @PostMapping("/image-to-text")
    public ResponseEntity<OperationLogDTO> recordImageToTextOperation(
            @RequestParam Long userId,
            @RequestParam Long imageId,
            @RequestParam Long textContentId) {
        OperationLogDTO log = operationLogService.recordImageToTextOperation(userId, imageId, textContentId);
        return ResponseEntity.ok(log);
    }

    /**
     * 记录图生图操作
     */
    @PostMapping("/image-to-image")
    public ResponseEntity<OperationLogDTO> recordImageToImageOperation(
            @RequestParam Long userId,
            @RequestParam Long imageId,
            @RequestParam Long generatedImageId) {
        OperationLogDTO log = operationLogService.recordImageToImageOperation(userId, imageId, generatedImageId);
        return ResponseEntity.ok(log);
    }

}
